package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.projectiles.ProjectileSource;

import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class BanPvpFlag extends JavaLandFlag implements Listener{
	private String tip;
	
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.NONE;
	}
	@Override
	public ELandFlagType getELandFlagType() {
		return ELandFlagType.PLAYER;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止PVP");
			config.set("lore", Arrays.asList("&7（禁止领地内的玩家进行PVP）"));
			config.set("tip", "&c领地标记限制，领地内的玩家不可以进行PVP");
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
		this.tip = LanguageManager.getChatPrefix() + Util.conver(config.getString("tip"));
	}
	
	private boolean isBanPVP(Player pl,Location loc) {
		final Land land = ILandManager.getILandManager().getHighestPriorityLand(loc);
		if(land == null) return true;
		return JavaLandFlag.hasLandFlag(land, this);
	}
	@EventHandler(priority = EventPriority.LOW,ignoreCancelled = true)
	public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
		if (!(e.getEntity() instanceof Player)) return;
		final Player player_attacker; //犯人
		if(e.getDamager() instanceof Projectile) { //如果犯人是投射物（例如箭）
			final ProjectileSource source = ((Projectile)e.getDamager()).getShooter(); //发射者
			if(source == null || !(source instanceof Player)) return; //犯人不是玩家
			player_attacker = (Player) source;
		}else {
			final Entity attkeren = e.getDamager();
			if(!(attkeren instanceof Player)) return;
			player_attacker = (Player) attkeren; //如果攻击者是玩家
		}
		//判断犯人和受害者
		if(!this.isBanPVP(player_attacker, player_attacker.getLocation()) || this.isBanPVP((Player)e.getEntity(),e.getEntity().getLocation())) {
			e.setCancelled(true);
			player_attacker.sendMessage(this.tip);
		}
	}
}